package com.sunshine.controller.admin;

import com.sunshine.authorization.annotation.Authorization;
import com.sunshine.bean.Information;
import com.sunshine.model.InformationMapper;
import com.sunshine.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.sunshine.util.Result.json;

/**
 * Created by along on 12/4/17.
 */
@RestController
@RequestMapping("/admin/information")
public class AdminInformationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdminInformationController.class);

    @Autowired
    private InformationMapper informationMapper;

    @PutMapping("/read")
    @Authorization
    public Map<String, Object> read(@RequestParam(value = "id") String id) {
        Information information = informationMapper.findById(id);
        if (information == null) {
            return json(Result.STATUS_CODE.SYSTEM_ERRO);
        } else {
            informationMapper.updateStatusById(id, Information.STATUS.PROCESSING.toString());
            return json(Result.STATUS_CODE.SUCCESS);
        }
    }

    @PutMapping
    @Authorization
    public Map<String, Object> responseInfo(@RequestParam(value = "id") String id,
                                            @RequestParam(value = "conductor") String conductor,
                                            @RequestParam(value = "response") String response) {
        Information information = informationMapper.findById(id);
        if (information == null) {
            return json(Result.STATUS_CODE.SYSTEM_ERRO);
        } else {
            information.setStatus(Information.STATUS.END.toString());
            information.setConductor(conductor);
            information.setResponse(response);
            return json(Result.STATUS_CODE.SUCCESS);
        }
    }

    @GetMapping("/status")
    @Authorization
    public Map<String, Object> getListByStatus(@RequestParam(value = "status") String status){
        List<Information> informationList = informationMapper.findByStatus(status);
        return json(Result.STATUS_CODE.SUCCESS,informationList);
    }

    @GetMapping("/type")
    @Authorization
    public Map<String, Object> getListByType(@RequestParam(value = "type") String type){
        List<Information> informationList = informationMapper.findByType(type);
        return json(Result.STATUS_CODE.SUCCESS,informationList);
    }
}
